package com.zy.batiya.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zy.batiya.domain.Price;
import com.zy.batiya.mapper.PriceMapper;
import com.zy.batiya.service.PriceService;
import com.zy.batiya.util.DateUtil;

@Transactional
@Component
public class PriceServiceImpl implements PriceService{
  
  private static final Logger logger = LoggerFactory.getLogger(PriceServiceImpl.class);
  
  @Autowired
  private PriceMapper priceMapper;

  @Override
  public void insert(Price price) {
    priceMapper.insert(price);
  }

  @Override
  public List<Price> findByPid(int pid) {    
    return priceMapper.findByPid(pid);
  }

  @Override
  public Price findById(int id) {
    return priceMapper.findById(id);
  }

  @Override
  public List<Price> search(String pid,String dt) {   
    return priceMapper.search(pid,dt);
  }

  @Override
  public void initPrice(String pid, String uid, String begin, String end, Double price) {
    try {
      List<String> dts = DateUtil.getDtList(begin, end, "yyyy-MM-dd");
      for(String dt:dts){
        Price priceObj = new Price();
        priceObj.setAmount(price.floatValue());
        priceObj.setDt(dt);
        priceObj.setPartner_pid(pid);
        priceObj.setPartner_uid(uid);
        
        List<Price> prices = priceMapper.search(pid,dt);
        if(prices != null && prices.size()>0){
          Price orignal = prices.get(0);
          orignal.setUpdatetime(new Date());
          orignal.setAmount(price.floatValue());
          priceMapper.update(orignal);
        }else{
          priceMapper.insert(priceObj);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}
